package com.yuexun.dhdoctor.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.util.InputMethodUtils;


/**
 * Created by wangt on 2017/8/21.
 */

public class EditTextDialog {
    private Activity activity;
    private Dialog dialog;
    private Display display;

    private TextView tv;
    private Button cancel;
    private Button sure;
    private EditText content;

    /**
     * 一。内部接口，要求实现一个callback方法
     *
     * @author Administrator cuiweiyou.com
     */
    private ICallBack icb;

    public EditTextDialog(Activity context) {
        this.activity = context;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public EditTextDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_edittext, null);

        tv = (TextView) view.findViewById(R.id.showTxt);
        cancel = (Button) view.findViewById(R.id.cancel);
        sure = (Button) view.findViewById(R.id.sure);
        content = (EditText) view.findViewById(R.id.ed_content);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                icb.cancelCallback();
            }
        });

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (icb != null) {
                    icb.sureCallback(content.getText().toString().trim());
                }
            }
        });

        // 设置Dialog最小宽度为屏幕宽度
        view.setMinimumWidth(display.getWidth());

        // 定义Dialog布局和参数
        dialog = new Dialog(activity, R.style.loadingDialogStyle);
        dialog.setContentView(view);
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                InputMethodUtils.closeSoftKeyboard(activity);
                            }
                        });
                    }
                }.start();
            }
        });

        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);

        return this;
    }

    /**
     * 三。设置回调变量
     *
     * @param cb
     */
    // 如果不是使用上面接收icallback参数的(二)构造方法创建此自定义对话框，则需要使用此方法指定icb变量
    public void setCallBack(ICallBack cb) {
        icb = cb;
    }

    public EditTextDialog setTitle(String title) {
        tv.setText(title);
        return this;
    }

    public EditTextDialog setContentHint(String hint) {
        content.setHint(hint);
        return this;
    }

    public EditTextDialog setCancel(String str) {
        cancel.setText(str);
        return this;
    }

    public EditTextDialog setSure(String str) {
        sure.setText(str);
        return this;
    }

    public EditTextDialog setCanceledOnTouchOutside(boolean is) {
        dialog.setCanceledOnTouchOutside(is);
        return this;
    }

    public EditTextDialog setCancelable(boolean is) {
        dialog.setCancelable(is);
        return this;
    }

    public void show() {
        dialog.show();
    }

    public void dismiss() {
        dialog.dismiss();
    }

    public interface ICallBack {
        void cancelCallback();

        void sureCallback(String content);
    }
}
